//
//  ZGLRechargeHeaderView.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/8/9.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLRechargeHeaderView.h"
@interface ZGLRechargeHeaderView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *acountLable; //账号
@property (nonatomic, strong) UILabel *yuELabel; //余额
@property (nonatomic, strong) UIView *infoBgView;
@property (nonatomic, strong) UITextField *customMoneyTF;
@property (nonatomic, strong) UILabel *chargeTipLable;
@property (nonatomic, strong) UIView *ButtonBgView;

@property (nonatomic, strong) NSArray *titleArray;        //金额
@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, strong) UIButton *selectedBtn;      //选中

@end

@implementation ZGLRechargeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = ViewBackgroundColor;
        
        UIView *infoBgView = [[UIView alloc]init];
        infoBgView.backgroundColor = [UIColor whiteColor];
        self.infoBgView = infoBgView;
        [self addSubview:self.infoBgView];
        [_infoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
//            make.height.equalTo(@(70));
//            make.bottom.equalTo(_yuELabel.mas_bottom);
        }];
        
        UILabel *acountLabel = [[UILabel alloc]init];
        
        NSString *acount = [PublicMethod phoneNumToAsterisk:sZGLUserInfoModel.mobile];
        acountLabel.text = [NSString stringWithFormat:@"账号：%@",acount];
        acountLabel.textAlignment = 0;
        acountLabel.font = [UIFont systemFontOfSize:15];
        self.acountLable = acountLabel;
        [_infoBgView addSubview:self.acountLable];
        [_acountLable mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self).offset(20);
            make.top.equalTo(self).offset(10);
        }];
        
        UILabel *yuELabel = [[UILabel alloc]init];
//        NSString *yuEStr = [PublicMethod DigitalNumberWithString:sZGLUserInfoModel.balance];
        yuELabel.text = [NSString stringWithFormat:@"余额：%@元",sZGLUserInfoModel.balance];
        yuELabel.textAlignment = 0;
        yuELabel.font = [UIFont systemFontOfSize:15];
        self.yuELabel = yuELabel;
        [_infoBgView addSubview:self.yuELabel];
        [_yuELabel mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.equalTo(_acountLable);
//            make.bottom.equalTo(_infoBgView);
            make.top.equalTo(_acountLable.mas_bottom).offset(20);

        }];
        
        [_infoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(_yuELabel.mas_bottom).offset(10);
        }];
        
        UIView *buttonBgView = [[UIView alloc]init];
        buttonBgView.backgroundColor = [UIColor whiteColor];
        self.ButtonBgView = buttonBgView;
        [self addSubview:self.ButtonBgView];
        [_ButtonBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(infoBgView.mas_bottom).offset(10);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
//            make.height.equalTo(@(40));
            
        }];
        
        UILabel *chargeTipLable = [[UILabel alloc]init];
        chargeTipLable.textAlignment = 0;
        chargeTipLable.text = @"请选择充值金额";
        chargeTipLable.font = [UIFont systemFontOfSize:14];
        self.chargeTipLable = chargeTipLable;
        [self addSubview:self.chargeTipLable];
        [_chargeTipLable mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_ButtonBgView).offset(10);
            make.left.equalTo(_ButtonBgView).offset(20);
            make.height.equalTo(@(20));
            
        }];
        
        [self creatMoneyButtonWith:self.titleArray];
    }
    
    return self;
}

- (void)creatMoneyButtonWith:(NSArray *)moneyArr
{
    NSInteger count = moneyArr.count;
    
    for (int i = 0; i<count+1; i++) {
        
        if (i<count) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_mark_bottom_right_normal"] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [button setTitleColor:globalRed forState:UIControlStateSelected];
            [button setTitleColor:RGBACOLOR(246, 63, 63, 1.0) forState:UIControlStateSelected];
            button.titleLabel.font = [UIFont systemFontOfSize:btnFontSize];
            button.layer.borderWidth = kBorderWidth;
            button.layer.borderColor = BorderColor.CGColor;
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
            button.tag = i;
            [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(chooceMark:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.btnArray addObject:button];
            [self.ButtonBgView addSubview:button];
            
        }else if (i == 5){
            
            UITextField *customMoneyTF = [[UITextField alloc]init];
            customMoneyTF.delegate = self;
            customMoneyTF.textAlignment = 1;
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.alignment = NSTextAlignmentCenter;
            NSAttributedString *attri = [[NSAttributedString alloc] initWithString:@"自定义金额" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:style}];
            customMoneyTF.attributedPlaceholder = attri; //placeholderz居中
            customMoneyTF.keyboardType = UIKeyboardTypeNumberPad;
            customMoneyTF.layer.borderWidth = kBorderWidth;
            customMoneyTF.layer.borderColor = BorderColor.CGColor;
            customMoneyTF.layer.cornerRadius = 5;
            customMoneyTF.layer.masksToBounds = YES;
            self.customMoneyTF = customMoneyTF;
            [self.ButtonBgView addSubview:customMoneyTF];
        }
        
    }
    
    [self.ButtonBgView.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:40 fixedLineSpacing:10 fixedInteritemSpacing:10 warpCount:3 topSpacing:40 bottomSpacing:10 leadSpacing:20 tailSpacing:20];
    
}

- (void)chooceMark:(UIButton *)sender
{
    NSLog(@"点击了%@", sender.titleLabel.text);
    
    self.selectedBtn = sender;
    
    sender.selected = !sender.selected;
    
    for (NSInteger j = 0; j < [self.btnArray count]; j++) {
        UIButton *btn = self.btnArray[j] ;
        if (sender.tag == j) {
            btn.selected = sender.selected;
        } else {
            btn.selected = NO;
        }
        btn.layer.borderColor = BorderColor.CGColor;
    }
    
    UIButton *btn = self.btnArray[sender.tag];
    if (btn.selected) {
        btn.layer.borderColor = globalRed.CGColor;
        self.customMoneyTF.text = @"";
        self.customMoneyTF.layer.borderColor = BorderColor.CGColor;
    } else {
        btn.layer.borderColor = BorderColor.CGColor;
        
    }
    
    DLog(@"self.btnArray--%@",self.btnArray);
    //返回金额
    if (self.moneyBlock) {
        self.moneyBlock(_selectedBtn.titleLabel.text);
    }
    
    
}

#pragma makr - UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    DLog(@"自定义金额");
    for (int i = 0; i<self.btnArray.count; i++) {
        UIButton *btn = self.btnArray[i];
        btn.layer.borderColor = BorderColor.CGColor;
    }
    
    DLog(@"_ButtonBgView-%@",self.ButtonBgView.subviews);
    for (UIView *view in self.ButtonBgView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            
            UIButton *button = (UIButton *)view;
            [button setSelected:NO];
            
            
        }else if ([view isKindOfClass:[UITextField class]]){
            
            UITextField *TF = (UITextField *)view;
            TF.layer.borderColor = globalRed.CGColor;
            
        }
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    DLog(@"填完金额");
    if (textField.text.length <= 0) {
        
        textField.layer.borderColor = BorderColor.CGColor;
    }
    
    if (textField.text.length > 0 ) {
        
        if (self.moneyBlock) {
            self.moneyBlock(textField.text);
        }
    }
}

- (NSArray *)titleArray
{
    if (!_titleArray) {
        NSArray *array = [NSArray array];
        array = @[@"50",@"100",@"200",@"300",@"500"];
        _titleArray = array;
    }
    return _titleArray;
}

- (NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc]init];
    }
    return _btnArray;
}

@end
